#include "heroplane.h"

HeroPlane::HeroPlane()
{
    //加载飞机图片
    m_Plane.load(HERO_PATH);

    m_Plane = m_Plane.scaled(100, 100,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);


    //初始化飞机坐标
    m_X = (GAME_WIDTH - m_Plane.width()) / 2;
    m_Y = GAME_HEIGHT- m_Plane.height();

    //初始化矩形框
    m_Rect.setWidth(m_Plane.width());
    m_Rect.setHeight(m_Plane.height());

    m_Rect.moveTo(m_X, m_Y);
}

void HeroPlane::shoot()
{

    //累加时间间隔记录变量
    m_recorder++;

    //如果记录的数字，未达到发射子弹时间间隔，直接return， 不发射子弹
    if(m_recorder < BULLET_INTERVAL)
    {
        return;
    }

    //达到发射时间
    m_recorder = 0;
    //发射子弹
    for(int i = 0; i<BULLET_NUM; i++)
    {
        //如果是空闲的子弹就发射

        if(m_bullets[i].m_Free)
        {
            //将空闲状态设置为假
            m_bullets[i].m_Free = false;

            //设置子弹坐标
            m_bullets[i].m_X = m_X + m_bullets[i].m_Bullet.width()/2;
            m_bullets[i].m_Y = m_Y + m_bullets[i].m_Bullet.height()/2;
            break;

        }

    }


}

void HeroPlane::setPosition(int x, int y)
{
    m_X = x;
    m_Y = y;

    m_Rect.moveTo(m_X, m_Y);

}
